/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.paris5.master2.guitarmaster.entity;

import com.paris5.master2.guitarmaster.bean.EcomUser;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.Size;

/**
 *
 * @author mourad
 */
@Entity
@Table(name = "user")
@NamedQueries({
    @NamedQuery(name="User.authentificate", query = "SELECT u FROM User u WHERE u.email=?1 AND u.password=?2 AND u.status=?3"),
    @NamedQuery(name="User.findByEmail", query = "SELECT u FROM User u WHERE u.email=?1")
})
public class User implements Serializable {
    
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "id_user")
    private Integer idUser;
    
    @Size(max = 100)
    @Column(name = "lastName")
    private String lastName;
    
    @Size(max = 100)
    @Column(name = "firstName")
    private String firstName;
    
    @Size(max = 20)
    @Column(name = "password")
    private String password;
    @Lob
    @Size(max = 65535)
    @Column(name = "adresse")
    private String adresse;
    
    @Size(max = 10)
    @Column(name = "cellular")
    private String cellular;
    
    // @Pattern(regexp="^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$", message="Invalid phone/fax format, should be as xxx-xxx-xxxx")//if the field contains phone or fax number consider using this annotation to enforce field validation
    @Size(max = 10)
    @Column(name = "phone")
    private String phone;
    
    // @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation
    @Size(max = 100)
    @Column(name = "email")
    private String email;
    
    @JoinColumn(name = "id_score", referencedColumnName = "id_score")
    @ManyToOne(cascade= CascadeType.REFRESH)
    private Score score;
    
    @Column(name = "creat_date")
    @Temporal(TemporalType.TIMESTAMP)
    private Date creatDate;
    
    @Column(name = "update_date")
    @Temporal(TemporalType.TIMESTAMP)
    private Date updateDate;
    
    @Size(max = 400)
    @Column(name = "photo")
    private String photo;
    
    @JoinColumn(name = "code", referencedColumnName = "code")
    @ManyToOne
    private Country code;

    @Column(name = "balance")
    private Float balance;

    @Column(name = "status")
    private Integer status;

    public User() {
    }
    
    public User(EcomUser eu){
        if(eu.getId()!=null){
            this.idUser = eu.getId();
        }
        lastName = eu.getLastName();
        firstName=eu.getFirstName();
        password=eu.getPassword();
        adresse=eu.getAdresse();
        cellular=eu.getCellular();
        phone=eu.getPhone();
        email=eu.getEmail();
        score=new Score(eu.getScore());
        creatDate=eu.getCreateDate();
        updateDate=eu.getUpdateDate();
        photo=eu.getPhoto();
        status=eu.getStatus();
        if(eu.getCountry()!=null)
            code = new Country(eu.getCountry());
        balance=eu.getBalance();
    }
    
    public EcomUser getEcomUser(){
        EcomUser user = new EcomUser();
        user.setLastName(this.getLastName());
        user.setId(idUser);
        user.setFirstName(firstName);
        user.setPassword(password);
        user.setAdresse(adresse);
        user.setCellular(cellular);
        user.setPhone(phone);
        user.setEmail(email);
        user.setScore(score.getEcomScore());
        user.setCreateDate(creatDate);
        user.setUpdateDate(updateDate);
        user.setPhoto(photo);
        user.setCountry(code.getEcomCountry());
        user.setBalance(balance);
        return user;
    }

    public User(Integer idUser) {
        this.idUser = idUser;
    }

    public Integer getIdUser() {
        return idUser;
    }

    public void setIdUser(Integer idUser) {
        this.idUser = idUser;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Float getBalance() {
        return balance;
    }

    public void setBalance(Float balance) {
        this.balance = balance;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getAdresse() {
        return adresse;
    }

    public void setAdresse(String adresse) {
        this.adresse = adresse;
    }

    public String getCellular() {
        return cellular;
    }

    public void setCellular(String cellular) {
        this.cellular = cellular;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Score getScore() {
        return score;
    }

    public void setScore(Score score) {
        this.score = score;
    }

    public Date getCreatDate() {
        return creatDate;
    }

    public void setCreatDate(Date creatDate) {
        this.creatDate = creatDate;
    }

    public Date getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    public Country getCode() {
        return code;
    }

    public void setCode(Country code) {
        this.code = code;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getStatus() {
        return status;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final User other = (User) obj;
        if (this.idUser != other.idUser && (this.idUser == null || !this.idUser.equals(other.idUser))) {
            return false;
        }
        if ((this.password == null) ? (other.password != null) : !this.password.equals(other.password)) {
            return false;
        }
        if ((this.email == null) ? (other.email != null) : !this.email.equals(other.email)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 29 * hash + (this.idUser != null ? this.idUser.hashCode() : 0);
        hash = 29 * hash + (this.password != null ? this.password.hashCode() : 0);
        hash = 29 * hash + (this.email != null ? this.email.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        return "com.paris5.master2.guitarmaster.entity.User[ idUser=" + idUser + " ]";
    }
    
}
